<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%> 

  			<div class="" style="padding: 5px;"> 
				<button id="id-btn-dialog1" class="btn btn-sm btn-primary">增加用户</button>
				<button id="id-btn-dialog2" class="btn btn-sm btn-primary">编辑用户</button>
				<button id="id-btn-dialog3" class="btn btn-sm btn-primary">删除用户</button>
				<button id="id-btn-dialog4" class="btn btn-sm btn-warning" onclick="window.user.disDelete(1)">启用</button>
				<button id="id-btn-dialog5" class="btn btn-sm btn-danger" onclick="window.user.disDelete(0)">禁用</button>
				<div style="float:right;">
					<input type="text" id="serch_text" style="width:250px" placeholder="用户名，显示名，电话，英文名">
				</div>
			</div>
			<table class="table table-striped table-hover table-bordered">
					  <thead>
						<tr id="sys_user_table_title">
						  <th  width="20">编号</th>
						  <th  width="20" fname="userNameCheckBox"><input id="userNameCheckBox" type="checkbox"></th>
						  <th  width="60" fname="userName">登陆名称</th> 
						  <th  width="60" fname="userShowName">显示名称</th>
						  <th  width="60" fname="userEngListName">英文名称</th>
						  <th  width="40" fname="age">年龄</th>
						  <th  width="40" fname="sex">性别</th>
						  <th  width="70" fname="tell">电话</th>
						  <th  width="70" fname="userEmail">邮件</th>
						  <th  width="50" fname="userType">类型</th> 
						  <th  width="50" fname="userImage">头像</th>
						  <th  width="100" fname="note">座右铭</th>
						  <th  width="40" fname="state">状态</th>
						  <th  width="100" fname="showTime">创建时间</th> 
						</tr>
					  </thead>
					  <tbody id="sys_user_table">
						 
					  </tbody>
					</table> 
					<ul class="pagination" id="user_pagination">
					  
					</ul> 		
<script type="text/javascript">
	(function() { 
		
		
			//改变他的启用禁用的状态
			function changeUserState(){
				var allstate =$("div[name='sys_user_table_state_tr_td']");
				for(var i=0;i<allstate.length;i++){
					var bc = $(allstate[i]);
					if(bc.text()==1){
						bc.html('<font color="green">启用</font>');
					}else if(bc.text()==0){
						bc.html('<font color="red">禁用</font>');
					}
				}
				$("#sys_user_table").children().click(function(){
					if($(this).find("[name=sys_user_table_checkbox]")[0].checked==true){
						$(this).find("[name=sys_user_table_checkbox]")[0].checked=false;
					}else{
						$(this).find("[name=sys_user_table_checkbox]")[0].checked=true;
					}
				});
			};
			//改变头像 和性别，还有类型
			function changeUserImage(){ 
				var allstate =$("div[name='sys_user_table_userImage_tr_td']");
				for(var i=0;i<allstate.length;i++){
					var bc = $(allstate[i]);
					bc.html('<img src="'+bc.text()+'" width="25" height="25"/>');
				}
				var allstate =$("div[name='sys_user_table_sex_tr_td']");
				for(var i=0;i<allstate.length;i++){
					var bc = $(allstate[i]);
					if(bc.text()==1){
						bc.html('<font color="green">男</font>');
					}else if(bc.text()==0){
						bc.html('<font color="red">女</font>');
					}
				}
				var allstate =$("div[name='sys_user_table_userType_tr_td']");
				for(var i=0;i<allstate.length;i++){
					var bc = $(allstate[i]);
					if(bc.text()==4){
						bc.html('<font color="green">助教</font>');
					}else if(bc.text()==6){
						bc.html('<font color="red">管理员</font>');
					}
				}
			}
			//全选中事件
			$("#userNameCheckBox").click(function(){
				var allc =$("[name=sys_user_table_checkbox]");
				for(var i=0;i<allc.length;i++){
					$(allc[i])[0].checked=$(this)[0].checked;
				}
					 
			});
			//用户搜索
			function loadUser(event,data){
				if(typeof(data)=="undefined"){
					data={};
				}
				var url ="${pageContext.request.contextPath}/edu/sysUserAction.do?method=get64User";
				data.userName=$("#serch_text").val();
				data.tell=$("#serch_text").val();
				data.userShowName=$("#serch_text").val();
				data.userEngListName=$("#serch_text").val();
				var cp =$("#user_pagination").find(".active");
				data.start=1;
				if(cp.length>0){
					var ccp=$(cp[0]).text();
					if(parseInt(ccp)){
						data.start=ccp;
					}
				}  
				$("#table_user").wgrid({
							table_title:"sys_user_table_title",
							tbodyId:"sys_user_table",
							pagination:"user_pagination",
							url:url,
							checkbox:true,
							data:data,
							success:function(){
								changeUserState();
								changeUserImage();
							}
				});
			};
		 //显示增加页面
		 function showUserAdd(){
			    $("#user_include").html("");
			 	$("#user_include").load("${pageContext.request.contextPath}/userAdd.jsp?r="+Math.random(),{},function(){
			 		var dialog = $("#dialog-message").removeClass('hide').dialog({
						modal: true,
						title: "增加用户",
						width:800,
						close:function(){
							$(this).dialog('destroy'); 
							$("#user_include").html("");
						},
						title_html: true,
						buttons: [  
							{
								text: "提交",
								"class" : "btn btn-primary btn-xs",
								click: function() {
									window.userAdd.addUser(this);
								} 
							}
						]
					}); 
				}); 
		 };
		//弹出用户增加页面
		$("#id-btn-dialog1" ).on('click', function(e) {
			e.preventDefault();
			showUserAdd();
		});
		$("#id-btn-dialog2" ).on('click', function(e) {
			e.preventDefault();
			var allc =$("input[type=checkbox][name=sys_user_table_checkbox]:checked");
			if(allc.length>1||allc.length<=0){
				alert("只能选中一行进行操作!");
				return;
			}
			var table_id=$(allc[0]).parent().parent().attr("id");
			var cdata =$("#"+table_id).data(table_id);
			 $("#user_include").html("");
			$("#user_include").load("${pageContext.request.contextPath}/userAdd.jsp?r="+Math.random(),cdata,function(){
		 		var dialog = $("#dialog-message").removeClass('hide').dialog({
					modal: true,
					title: "修改用户",
					width:800,
					close:function(){
						$(this).dialog('destroy'); 
						$("#user_include").html("");
					},
					title_html: true,
					buttons: [  
						{
							text: "提交",
							"class" : "btn btn-primary btn-xs",
							click: function() {
								window.userAdd.addUser(this);
							} 
						}
					]
				}); 
			}); 
		});
		//搜索的回车事件
		$('#serch_text').keydown(function(e){
			if(e.keyCode==13){
				loadUser(null,{});
			}
		});
		//删除一个用户
		function userDelete(){
			var r=confirm("您确认要删除数据吗？")
			if (r==false){return}; 
			var allc =$("input[type=checkbox][name=sys_user_table_checkbox]:checked");
			if(allc.length<=0){
				alert("你没有选中用户,不能进行删除操作!");
				return;
			}
			var arr = [];
			for(var i=0;i<allc.length;i++){
				arr.push($(allc[i]).parent().parent().attr("id"));
			}
			$.ajax({
				type: "POST",
				url:"${pageContext.request.contextPath}/edu/sysUserAction.do?method=deleteUser",
				data:{allId:obj2str(arr)},
				dataType:'json',
				success: function(msg){
					alert(msg.message); 
					loadUser(null,{});
				}
			});	
			 
		};
		//启用和禁用一个用户
		function disDelete(temp){
			var allc =$("input[type=checkbox][name=sys_user_table_checkbox]:checked");
			if(allc.length<=0){
				alert("你没有选中用户,不能进行启用和禁用操作!");
				return;
			}
			var arr = [];
			for(var i=0;i<allc.length;i++){
				arr.push($(allc[i]).parent().parent().attr("id"));
			}
			$.ajax({
				type: "POST",
				url: "${pageContext.request.contextPath}/edu/sysUserAction.do?method=disUser",
				data:{allId:obj2str(arr),flag:temp},
				dataType:'json',
				success: function(msg){
					alert(msg.message); 
					loadUser(null,{});
				}
			});	
		};
		//删除选中用户
		$("#id-btn-dialog3" ).on('click', function(e) {
			e.preventDefault();
			userDelete();
		});
		
		$(function(){
			loadUser(null,{});
		}); 
		window.user={};
		window.user.loadUser=loadUser;
		window.user.disDelete=disDelete;
	})();
</script>

